package com.fanfan.SmartPensionSystembackend.service.impl;

import com.fanfan.SmartPensionSystembackend.entity.User;
import com.fanfan.SmartPensionSystembackend.mapper.UserMapper;
import com.fanfan.SmartPensionSystembackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 注册用户
    @Override
    public boolean registerUser(User user) {
        try {
            // 自动生成 user_id，格式为 0001, 0002, ...
            String userId = String.format("%04d", userMapper.selectCount(null) + 1);
            user.setUserId(userId);

            user.setCode(null); // 不需要将 code 字段插入到数据库中

            System.out.println("user:"+user);
            // 插入用户数据到数据库
            int result = userMapper.insert(user);
            return result > 0; // 返回插入是否成功
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 登录用户
    @Override
    public User loginUser(String username, String password) {
        try {
            // 根据用户名查询用户信息
            User user = userMapper.selectOneByUsername(username);
            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 更新用户密码
    @Override
    public boolean updatePassword(String username, String oldPassword, String newPassword) {
        try {
            // 根据用户名查询用户信息
            User user = userMapper.selectOneByUsername(username);
            if (user != null && user.getPassword().equals(oldPassword)) {
                // 更新密码
                user.setPassword(newPassword);
                int result = userMapper.updateByUsername(user);
                return result > 0; // 返回更新是否成功
            } else {
                return false; // 旧密码不正确
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取所有用户信息
    @Override
    public List<User> getAllUsers() {
        try {
            List<User> user = userMapper.selectAllUsers();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 更新用户信息
    @Override
    public boolean updateUser(User user) {
        try {
            int result = userMapper.updateById(user);
            return result > 0; // 返回更新是否成功
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除用户
    @Override
    public boolean deleteUser(String userId) {
        try {
            int result = userMapper.deleteById(userId);
            return result > 0; // 返回删除是否成功
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}